package com.webserver.controller;

import com.webserver.annotation.Controller;
import com.webserver.annotation.RequestMapping;
import com.webserver.http.HttpServletRequest;
import com.webserver.http.HttpServletResponse;
import qrcode.QRCodeUtil;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;

@Controller
public class ToolsController {
    @RequestMapping("/myweb/createQr")
    public void createQr(HttpServletRequest request, HttpServletResponse response){
        String content = request.getParameter("content");
        try {
            QRCodeUtil.encode(content,"kabi.jpg",response.getOutputStream(),true);
            response.setContentType("image/jpeg");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/myweb/random.jpg")
    public void createRandomImage(HttpServletRequest request, HttpServletResponse response){
        //1创建一张空图片，并且指定宽高。 理解为:创建一张画纸
        BufferedImage image =  new BufferedImage(70,30,BufferedImage.TYPE_INT_RGB);

        //2根据图片获取一个画笔，通过该画笔画的内容都会画到该图片上
        Graphics g = image.getGraphics();

        //3确定验证码内容(字母与数字的组合)
        String line = "abcdefghjiklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random = new Random();//用于生成随机数(随机数位line的字符下标)

        //为图片背景填充一个随机颜色
        //创建Color时，需要指定三个参数，分别是，红，绿，蓝。数字范围都是(0-255)之间
        Color bgcolor = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
        //将画笔设置为该颜色
        g.setColor(bgcolor);
        //填充整张图片为画笔当前颜色
        g.fillRect(0,0,70,30);

        //向图片上画4个字符
        for(int i=0;i<4;i++) {
            //随机生成一个字符
            String str = line.charAt(random.nextInt(line.length())) + "";
            //生成随机颜色
            Color color = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
            //设置画笔颜色
            g.setColor(color);
            //设置字体
            g.setFont(new Font(null, Font.BOLD, 20));
            //将字符串画到图片指定的位置上
            g.drawString(str, i*15+5, 18+ random.nextInt(11)-5);
        }

        //随机生成4条干扰线
        for (int i=0;i<4;i++){
            Color color = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
            g.setColor(color);
            g.drawLine(random.nextInt(71), random.nextInt(31),
                    random.nextInt(71), random.nextInt(31));
        }

        //将图片写入文件来生成该图片文件
        try {
            ImageIO.write(image,"jpg",response.getOutputStream());
            response.setContentType("image/jpeg");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) {
        BufferedImage image = new BufferedImage(70,30,BufferedImage.TYPE_INT_RGB);

        Graphics g = image.getGraphics();

        String line = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random r = new Random();

        Color bgcolor = new Color(r.nextInt(256),r.nextInt(256),r.nextInt(256));
        g.setColor(bgcolor);
        g.fillRect(0,0,70,30);

        for (int i = 0; i < 4; i++) {
            String str = line.charAt(r.nextInt(line.length()))+"";
            Color fontColor = new Color(r.nextInt(256),r.nextInt(256),r.nextInt(256));
            g.setColor(fontColor);
            g.setFont(new Font(null,Font.BOLD,20));
            g.drawString(str,i*15+5,18+r.nextInt(10)-5);
        }

        for (int i = 0; i < 4; i++) {
            Color lineColor = new Color(r.nextInt(256),r.nextInt(256),r.nextInt(256));
            g.setColor(lineColor);
            g.drawLine(r.nextInt(71),r.nextInt(31),r.nextInt(71),r.nextInt(31));
        }

        try {
            ImageIO.write(image,"jpg",new FileOutputStream("./random.jpg"));
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
